//
//  AppDelegate.swift
//  MicroBlogFunc
//
//  Created by GK on 16/4/8.
//  Copyright © 2016年 GK. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //设置全局barbuttonitem颜色
        let bbi = UIBarButtonItem.appearance()
        bbi.tintColor = UIColor.orangeColor()
        
        //创建窗口根控制器
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        
        //设置为窗口的根控制器
        let vc = defaultViewController()
        window?.rootViewController = vc
        
        //设置为主窗口,当window有值的时候才会继续往下执行
        window?.makeKeyAndVisible()
        
        return true
    }

    
    //程序启动时默认的控制器确定
    private func defaultViewController() -> UIViewController{
        //判断是否是新版本
        let isANewVersion: Bool = isNewVersion()
        
        if !GKUserAccountViewModel.shareInstance.userLogin
        {   //此时用户没有登录
            return GKTabBarController()
        }
        
        
        return isANewVersion ? GKNewFeatureViewController() : GKWelcomeViewController()
    }
    
    
    //判断是否是新版本
    private func isNewVersion() -> Bool {
        
        let versionKey = "versionKey"
        
        //获取当前版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! String
        //获取上一次版本号
        let lastVersion = NSUserDefaults.standardUserDefaults().stringForKey(versionKey)
        //比较版本号判断是否是新版本
        let isNewVer: Bool = currentVersion != lastVersion
        
        if isNewVer
        {   //假如是新版本,保存版本号
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: versionKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
//        GKPrint("是否是新版本\(isNewVer)")
        return isNewVer
    }
    
    
    //控制器切换
    func switchViewController(controller: UIViewController) {
        
        window?.rootViewController = controller
    }
    
    
    
    
    
    
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

